#ifndef SEQUENCE_H
#define SEQUENCE_H
#include <string>
#include <vector>

#include "StringUtility.h"
#include "WandorBaseState.h"

#include "Overlay.h"
#include "StatefulUnit.h"

class Sequence
{
    public:
        Sequence(string name);
        virtual ~Sequence();

        WandorBaseState *getLevel() { return m_level; }
        void setLevel(WandorBaseState *level) { m_level = level; }

        void update();  // Updates the counter, activates animations and other stuff

        void reset() { m_index = 0; m_active = false; m_waitingOnCounter = false; m_waitingOnUnit = false; m_waitingOn = NULL; m_counter.stop(); }
        void start() { m_index = 0; m_active = true; m_waitingOnCounter = false; m_waitingOnUnit = false; m_waitingOn = NULL; m_counter.stop(); }

        void addStep(string step) { m_steps.push_back(step); }
        void clear() { reset(); m_steps.clear(); }

        string getName() { return m_name; }
    protected:
        void customUpdate(); // Is called when the internal counter reaches zero
        CountDown m_counter; // Internal counter

        string m_name; // Name of sequence
        uint m_index; // Step to be executed
        BaseUnit *m_waitingOn; // Object we are waiting to finish
        bool m_waitingOnCounter;
        bool m_waitingOnUnit;
        bool m_active; // Are we live, are we performing steps

        string m_pendingAction; // This should only have a value if we are waiting on a counter; if it has a value and we are done waiting,
                                // we do it instead of doing the next m_index; then we increment the m_index.

        WandorBaseState *m_level;
        vector<string> m_steps;
    private:
        void performStep(string step, bool waitForCompletion);
        static void countDownWrapper(void* obj)
        {
            Sequence* self = (Sequence*) obj;
            self->customUpdate();
        }
};

#endif // SEQUENCE_H
